package com.bawei.live

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.VideoEntity
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.live.adapter.HotLiveAdapter
import com.bawei.live.adapter.LiverListAdapter
import com.bawei.live.adapter.TypeListAdapter
import com.bawei.live.databinding.FragmentLiveBinding
import com.bawei.live.entity.LiverEntity
import com.bawei.live.entity.TypeEntity
import com.bawei.live.viewmodel.LiveViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class LiveFragment : BaseMviFragment<FragmentLiveBinding>() {

    private val mViewModel: LiveViewModel by activityViewModels()

    private lateinit var mLiverAdapter: LiverListAdapter
    private lateinit var mTypeAdapter: TypeListAdapter
    private lateinit var mHotLiveAdapter: HotLiveAdapter
    private var liverList = mutableListOf<LiverEntity>()
    private var typeList = mutableListOf<TypeEntity>()
    private var hotLiveList = mutableListOf<VideoEntity>()


    override fun getLayoutId(): Int = R.layout.fragment_live

    override fun initViews() {
        //初始化主播列表
        mDataBinding.rvLiver.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        mLiverAdapter = LiverListAdapter(liverList)
        mDataBinding.rvLiver.adapter = mLiverAdapter

        //初始化热门分类列表
        mDataBinding.rvHotType.layoutManager = GridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
        mTypeAdapter = TypeListAdapter(typeList)
        mDataBinding.rvHotType.adapter = mTypeAdapter

        //初始化热门直播列表
        mDataBinding.rvHotLive.layoutManager = GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
        mHotLiveAdapter = HotLiveAdapter(hotLiveList)
        mHotLiveAdapter.setOnItemClickListener(object : HotLiveAdapter.OnItemClickListener{
            override fun onItemChildClick(v: View?, position: Int) {
                ARouter.getInstance().build("/video/play")
                    .withSerializable("videoEntity", hotLiveList[position])
                    .withInt("typeId", -1)
                    .withString("channelId", hotLiveList[position].channelid)
                    .navigation()
            }
        })

        mDataBinding.rvHotLive.adapter = mHotLiveAdapter

        //本地模拟直播和分类列表
        initLocData()
    }


    private fun initLocData(){
        //模拟主播数据
        liverList.add(LiverEntity(0, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(1, R.drawable.shape_liver_bg_2,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(2, R.drawable.shape_liver_bg_3,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(3, R.drawable.shape_liver_bg_4,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(4, R.drawable.shape_liver_bg_5,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        mLiverAdapter.notifyDataSetChanged()

        //模拟热门分类数据
        typeList.add(TypeEntity(0, R.drawable.type_game, "游戏区"))
        typeList.add(TypeEntity(1, R.drawable.type_music, "音乐区"))
        typeList.add(TypeEntity(2, R.drawable.type_flor, "生活区"))
        typeList.add(TypeEntity(3, R.drawable.type_car, "懂车帝"))
        mTypeAdapter.notifyDataSetChanged()

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(LiveViewIntent.LoadRecommendSimpleVideo(1, 10))
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect {
            when (it) {
                is LiveUiState.LoadSimpleVideoSuccess -> {
                   hotLiveList.addAll(it.list)
                   mHotLiveAdapter.notifyDataSetChanged()
                }
                is LiveUiState.LoadSimpleVideoFail -> showToastMessage(it.msg)
                else -> {}
            }
        }
    }
}